/* eslint-disable vue/no-parsing-error */
<template>
  <!-- <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">若依后台管理系统</h3>
      <el-form-item prop="username">
        <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
           <el-input
            placeholder="昵称"
            v-model="register.nickname"
            class="mymargin"
          >
          </el-input> -->
      <!-- </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input
          v-model="registerForm.confirmPassword"
          type="password"
          auto-complete="off"
          placeholder="确认密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaOnOff">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img"/>
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
      底部  
    <div class="el-register-footer">
      <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>  -->

  <div class="register">
    <el-form class="register-form">
      <el-row :gutter="20">
        <el-col :span="23">
          <div class="demo-input-suffix">
            
              <!-- <el-form-item>
                <el-input type="text" auto-complete="off" placeholder="账号">
                  <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item> -->
            <el-form-item>
              <h3 class="title">注册新账号</h3>
              <el-input 
                prefix-icon="el-icon-user"
                placeholder="昵称"
                v-model="register.nickname"
                class="mymargin"
              >
                <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
              </el-input>


              <el-input 
              prefix-icon="el-icon-user-solid"
              placeholder="手机号"
              v-model="register.mobile"
              class="mymargin"
              >
              </el-input>

              <el-input
                placeholder="密码：请输入6-20个字母、数字、下划线 "
                v-model="register.password"
                prefix-icon="el-icon-lock"
                type="password"
                class="mymargin"
              >
              </el-input>

              <el-input prefix
                placeholder="确认密码"
                type="password"
                prefix-icon="el-icon-lock"
                v-model="repassword"
                class="mymargin"
              >
                <!-- <i slot="prefix" class="el-input__icon el-icon-search"></i> -->
              </el-input>

              <el-input
                placeholder="短信验证码"
                prefix-icon="el-icon-message"
                v-model="code"
                class="mymargin"
              >
                <el-button
                  slot="append"
                  class="mymargin"
                  @click="sendsms()"
                >获取短信验证码</el-button>
              </el-input>
              <el-checkbox
                v-model="checked"
                class="mymargin"
              > 同意协议并接受《服务条款》</el-checkbox>
              <el-button
                  type="primary"
                  style="float:right;"
                  class="mymargin"
                  @click.prevent="regist()"
                >注册</el-button>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div class="el-register-footer">
      <span>Copyright © 2021-2022 yunche.vip All Rights Reserved.</span>
    </div>
  </div>
</template>
<script>
import userApi from "~/api/user.js";
import axios from "axios";
import Vue from "vue";
import Auth from "@/utils/auth";
export default {
  layout:'login_default',
  data() {
    return {
      checked: false,
      register: {},
      user: {},
      code: "",
      repassword: ""
    };
  },
  methods: {
    regist() {
      if (
        this.register.nickname == undefined ||
        this.register.nickname.trim().length < 1
      ) {
        this.$message({
          message: "请输入昵称",
          type: "error"
        });
        return;
      }
      const mobileRule = /(?:^1[3456789]|^9[28])\d{9}$/;
      if (!mobileRule.test(this.register.mobile)) {
        this.$message({
          message: "请输入正确手机号",
          type: "error"
        });
        return;
      }
      if (this.code == "") {
        this.$message({
          message: "请输入验证码",
          type: "error"
        });
        return;
      }

      if (this.register.password != this.repassword) {
        this.$message({
          message: "两次密码输入不一致",
          type: "error"
        });
        return;
      }
      var patrn = /^(\w){6,20}$/;
      if (!patrn.exec(this.register.password)) {
        this.$message({
          message: "请输入6-20个字母、数字、下划线密码",
          type: "error"
        });
        return;
      }
      userApi.register(this.register, this.code).then(res => {
        if (res.data.flag) {
          this.$message({
            message: "注册成功",
            type: "success"
          });
          this.pojo = {};
          this.$router.push('/login');
        } else {
          this.$message({
            message: res.data.message,
            type: "error"
          });
        }
        this.register = {};
      });
    },
    sendsms() {
      const mobileRule = /(?:^1[3456789]|^9[28])\d{9}$/;
      if (!mobileRule.test(this.register.mobile)) {
        this.$message({
          message: "请输入正确手机号",
          type: "error"
        });
        return;
      }

      userApi.sendsms(this.register.mobile).then(res => {
        if (res.data.flag) {
          this.$message({
            message: res.data.message,
            type: "success"
          });
          this.pojo = {};
        } else {
          this.$message({
            message: "发送短信验证码失败",
            type: "error"
          });
        }
      });
    },
    login() {
      const mobileRule = /(?:^1[3456789]|^9[28])\d{9}$/;
      if (!mobileRule.test(this.user.mobile)) {
        this.$message({
          message: "请输入正确手机号",
          type: "error"
        });
        return;
      }
      var patr = /^(\w){6,20}$/;
      if (!patr.exec(this.user.password)) {
        this.$message({
          message: "请输入6-20个字母、数字、下划线密码",
          type: "error"
        });
        return;
      }
      userApi.login(this.user).then(res => {
        if (res.data.flag) {
          Auth.setUser(
            res.data.data.token,
            res.data.data.nickname,
            res.data.data.avatar
          );
          location.href = "/";
        } else {
          this.$message({
            message: res.data.message,
            type: "error"
          });
          // this.mobile=''
          this.password = "";
        }
        //   this.user={}
      });
    }
  }
};
</script>

<style>
.loginsign-title {
  margin-bottom: 20px;
  font-weight: normal;
  font-size: 20px;
  color: #676666;
}

.mymargin {
  margin: 10px 10px 10px 10px;
}

.register {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  width: 100%;
  height: 100%;
  background-image: url("../assets/img/bg-4.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 16px auto;
  text-align: center;
  font-size: 25px;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  /* .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  } */
  /* .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  } */
}

.register-form .el-input {
    height: 38px;
    /* input {
      height: 38px;
    } */
}
.register-form .el-input input {
  height: 38px;
}
.register-form .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  /* img {
    cursor: pointer;
    vertical-align: middle;
  } */
}
.register-code img {
  cursor: pointer;
  vertical-align: middle;
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 16px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
<style scoped>

</style>
